package com.example.cookbook.fragment;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.cookbook.MyDBHelper;
import com.example.cookbook.R;
import com.example.cookbook.adapter.CourseSCAdapter;
import com.example.cookbook.bean.RecipeBean;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class TwoFragment extends Fragment {
    private RecyclerView recy_view2;
    private MyDBHelper mhelper;
    private SQLiteDatabase db;
    ArrayList<RecipeBean> arrlist3;

    public TwoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        initView(view);
        return view;

    }
    private void initView(View view) {
        recy_view2 = (RecyclerView) view.findViewById(R.id.recy_view2);
        mhelper = new MyDBHelper(getActivity());
        db = mhelper.getWritableDatabase();
    }

    /** @noinspection deprecation, deprecation */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();      //数据的初始化
    }
    private void initData() {
        //获取数据
        arrlist3 = new ArrayList<RecipeBean>();
        Cursor cursor = db.rawQuery("select * from tb_recipe", null);
        int indexName = cursor.getColumnIndex("recipeName");
        int indexId = cursor.getColumnIndex("recipeId");
        int indexDesc = cursor.getColumnIndex("recipeDesc");
        int indexSmallImg = cursor.getColumnIndex("recipeSmallImgUrl");

        // 确保所有需要的列都存在
        if (indexName >= 0 && indexId >= 0 && indexDesc >= 0 && indexSmallImg >= 0) {
            while (cursor.moveToNext()) {
                String name = indexName >= 0 ? cursor.getString(indexName) : "";
                String resId = indexId >= 0 ? cursor.getString(indexId) : "";
                String desc = indexDesc >= 0 ? cursor.getString(indexDesc) : "";
                String smallImg = indexSmallImg >= 0 ? cursor.getString(indexSmallImg) : "";

                RecipeBean bean = new RecipeBean();
                bean.setCpName(name);
                bean.setId(resId);
                bean.setDes(desc);
                bean.setSmallImg(smallImg);
                arrlist3.add(bean);
            }
        } else {
            // 如果有任何必要的列不存在，根据情况处理，比如记录错误日志或抛出异常
            Log.e("CursorError", "One or more required columns are missing from the query result.");
        }

        cursor.close();
        //创建自定义适配器的对象
        CourseSCAdapter adapter = new CourseSCAdapter(getActivity(), arrlist3);
        //用网格布局管理器将数据显示出来
        StaggeredGridLayoutManager st = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        recy_view2.setLayoutManager(st);
        recy_view2.setAdapter(adapter);
    }

    @Override
    //退出时，回收资源
    public void onResume() {
        super.onResume();
        arrlist3.clear();
        initData();
    }

}
